package com.geline.easyexcel.domain;

import com.geline.easyexcel.annotation.ExcelDropDown;
import com.geline.easyexcel.compute.ArrayComputeUtil;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.Collection;

/**
 * 下拉参数定义
 *
 * @author: mengxin
 * @date: 2024/1/8 20:36
 */
@Getter
@Setter
public class DropDownItem implements Serializable {

    //字典名称
    private String dictName;
    //字典代码
    private String dictCode;
    //标签页名称
    private ExcelDropDown dropDown;
    //静态下拉内容
    private Collection<String> itemList;
    //公式 INDIRECT($N2)
    private String formula;

    public DropDownItem(ExcelDropDown dropDown, ParentDict bean){
        this.dropDown = dropDown;
        this.dictName = bean.getDictName();
        this.dictCode = bean.getDictCode();
        if(dropDown.type()== DropDownType.CASCADE){
            this.itemList = bean.getItemList();
            int parentIndex = dropDown.parentIndex();
            if(parentIndex==-1){
                throw new RuntimeException(" parentIndex=-1 is not allow when DropDownType = CASCADE !");
            }else {
                this.formula = dropDown.type().toFormulaString(parentIndex+1);
            }
        }else if(dropDown.type()== DropDownType.CHECKBOX){
            //生成所有可能的勾选组合列表
            this.itemList = ArrayComputeUtil.sortByString(bean.getItemList().toArray(new String[0]), true);
            this.formula = dropDown.type().toFormulaString(dropDown.dictSheet(), dropDown.dictName());
        }else {
            this.itemList = bean.getItemList();
            this.formula = dropDown.type().toFormulaString(dropDown.dictSheet(), dropDown.dictName());
        }
    }
}
